//
//  UILabel+Category.m
//  XKOneHun
//
//  Created by xyg on 2016/12/8.
//  Copyright © 2016年 xiaokang100. All rights reserved.
//

#import "UILabel+Category.h"
#import <CoreText/CoreText.h>

@implementation UILabel (Category)


+ (instancetype)getLabWithText:(NSString *)text textFont:(CGFloat)font {
    return [self getLabWithText:text textFont:font textColor:[UIColor blackColor] textAlignment:NSTextAlignmentLeft numberOfLines:1];
}

+ (instancetype)getLabWithText:(NSString *)text textFont:(CGFloat)font textColor:(UIColor *)color {
    return [self getLabWithText:text textFont:font textColor:color textAlignment:NSTextAlignmentLeft numberOfLines:1];
}

+ (instancetype)getLabWithText:(NSString *)text textFont:(CGFloat)font textColor:(UIColor *)color textAlignment:(NSTextAlignment)textAlignment {
    return [self getLabWithText:text textFont:font textColor:color textAlignment:textAlignment numberOfLines:1];
}

+ (instancetype)getLabWithText:(NSString *)text textFont:(CGFloat)font textColor:(UIColor *)color textAlignment:(NSTextAlignment)textAlignment numberOfLines:(CGFloat)numberOfLines {
    return [self getLabWithText:text textFont:font textColor:color textAlignment:textAlignment numberOfLines:numberOfLines lineBreakMode:NSLineBreakByTruncatingTail];
}

+ (instancetype)getLabWithText:(NSString *)text textFont:(CGFloat)font textColor:(UIColor *)color textAlignment:(NSTextAlignment)textAlignment numberOfLines:(CGFloat)numberOfLines lineBreakMode:(NSLineBreakMode)lineBreakMode {
    UILabel *lab = [UILabel new];
    lab.text = text;
    lab.font = [UIFont systemFontOfSize:font];
    lab.textAlignment = textAlignment;
    lab.numberOfLines = numberOfLines;
    lab.textColor = color;
    lab.lineBreakMode = lineBreakMode;
    return lab;
}

- (void)setColumnSpace:(CGFloat)columnSpace
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    //调整间距
    [attributedString addAttribute:(__bridge NSString *)kCTKernAttributeName value:@(columnSpace) range:NSMakeRange(0, [attributedString length])];
    self.attributedText = attributedString;
}

- (void)setRowSpace:(CGFloat)rowSpace
{
    self.numberOfLines = 0;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    //调整行距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = rowSpace;
    paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
    self.attributedText = attributedString;
}

@end
